﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ToolS
{
    public class LoadUrls
    {
        static string path = Init.workPath + "/urls";
        public static List<urls> LoadUrlsFromFile()
        {
            if (!File.Exists(path))
            {
                File.Create(path);
                return null;
            }
            string strUrl = FileHelper.GetFileString(path);

            var result = XmlHelper.Deserialize<List<urls>>(strUrl);
            return result;
        }

        public static bool AddUrl(string url)
        {
            if (!File.Exists(path))
            {
                File.Create(path);
            }

            string strUrl = FileHelper.GetFileString(path);

            var result = XmlHelper.Deserialize<List<urls>>(strUrl);

            if (result == null)
            {
                result = new List<urls>();
            }
            //result.Add(new urls { url = url });
            if (result.Any(item => item.url.Equals(url, StringComparison.OrdinalIgnoreCase)))
            {
                return false;
            }
            FileHelper.WriteFile(path, XmlHelper.ObjectSerialize(result));

            return true;
        }
    }
}
